package com.kalaqia.wechat.pay;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WeChatPayReq implements IWXAPIEventHandler {
	
	private static final String TAG = WeChatPayReq.class.getSimpleName();

	private Activity mActivity;

	private String appId;
	private String partnerId;
	private String prepayId;
	private String packageValue;
	private String nonceStr;
	private String timeStamp;
	private String sign;
	private IWXAPI mWXApi;
	
	public void send() {
        mWXApi = WXAPIFactory.createWXAPI(mActivity, null);
        mWXApi.handleIntent(mActivity.getIntent(), this);
        
        mWXApi.registerApp(this.appId);
        
        PayReq request = new PayReq();

        request.appId = this.appId;
        request.partnerId = this.partnerId;
        request.prepayId= this.prepayId;
        request.packageValue = this.packageValue != null ? this.packageValue : "Sign=WXPay";
        request.nonceStr= this.nonceStr;
        request.timeStamp= this.timeStamp;
        request.sign = this.sign;
        
        mWXApi.sendReq(request);
	}
	
	public static class Builder{
		private Activity activity;
		private String appId;
		private String partnerId;
		private String prepayId;
		private String packageValue="Sign=WXPay";
		private String nonceStr;
		private String timeStamp;
		private String sign;

		public Builder with(Activity activity){
			this.activity = activity;
			return this;
		}
		
		public Builder setAppId(String appId){
			this.appId = appId;
			return this;
		}
		
		public Builder setPartnerId(String partnerId){
			this.partnerId = partnerId;
			return this;
		}
		
		public Builder setPrepayId(String prepayId){
			this.prepayId = prepayId;
			return this;
		}

		public Builder setPackageValue(String packageValue){
			this.packageValue = packageValue;
			return this;
		}

		public Builder setNonceStr(String nonceStr){
			this.nonceStr = nonceStr;
			return this;
		}
		
		public Builder setTimeStamp(String timeStamp){
			this.timeStamp = timeStamp;
			return this;
		}
		
		public Builder setSign(String sign){
			this.sign = sign;
			return this;
		}

		public WeChatPayReq create(){
			WeChatPayReq wechatPayReq = new WeChatPayReq();
			
			wechatPayReq.mActivity = this.activity;
			wechatPayReq.appId = this.appId;
			wechatPayReq.partnerId = this.partnerId;
			wechatPayReq.prepayId = this.prepayId;
			wechatPayReq.packageValue = this.packageValue;
			wechatPayReq.nonceStr = this.nonceStr;
			wechatPayReq.timeStamp = this.timeStamp;
			wechatPayReq.sign = this.sign;
			
			return wechatPayReq;
		}
		
	}
	
	//微信支付监听
	private OnWeChatPayListener mOnWeChatPayListener;

	public WeChatPayReq setOnWeChatPayListener(OnWeChatPayListener onWechatPayListener) {
		this.mOnWeChatPayListener = onWechatPayListener;
		return this;
	}

	public interface OnWeChatPayListener {
		void onPaySuccess(int errorCode);
		void onPayFailure(int errorCode);
	}

	@Override
	public void onReq(BaseReq baseReq) {
		Toast.makeText(this.mActivity, "onReq===>>>get baseReq.getType : "+baseReq.getType(), Toast.LENGTH_LONG).show();
        Log.d(TAG,"onReq===>>>get baseReq.getType : "+baseReq.getType());
	}


	@Override
	public void onResp(BaseResp resp) {
        Toast.makeText(this.mActivity, "onResp===>>>get resp.getType : "+ resp.getType(), Toast.LENGTH_LONG).show();
        if(resp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
            Log.d(TAG,"onPayFinish,errCode="+resp.errCode);
            if(this.mOnWeChatPayListener != null){
            	if(resp.errCode == BaseResp.ErrCode.ERR_OK){
            		this.mOnWeChatPayListener.onPaySuccess(resp.errCode);
            	}else{
            		this.mOnWeChatPayListener.onPayFailure(resp.errCode);
            	}
            }
        }
	}
}
